package asset;

import java.util.ArrayList;
import java.util.List;

/**
 * Regist : 负责注册和检验指令
 */
public class RegOrder {

    private String machineCodeString = "";
    private String disasmbCodeString = "";

    public RegOrder() {
    }

    public RegOrder(String disasmbCodeString) {
        this.disasmbCodeString = disasmbCodeString;
    }

    public RegOrder(String machineCodeString,String disasmbCodeString) {
        this.disasmbCodeString = disasmbCodeString;
        this.machineCodeString = machineCodeString;
    }

    public String getMachineCodeString() {
        return machineCodeString;
    }

    public String getDisasmbCodeString() {
        return disasmbCodeString;
    }

    public static boolean checkMachineInst(String codeString) {
        if (codeString.matches("^((0x)?[0-9a-f]{8})$")) {
            return true;
        }
        if (codeString.matches("^((0b)?[01]{32})$")) {
            return true;
        }
        return false;
    }

    /** only hex and binary are allowed 
     * @return 32 bit binary code
    */
    public static String filterMachineCode(String codeString) {
        String strFilter = codeString.strip();

        //prefix filter
        if (strFilter.length() == 10 || strFilter.length() == 34) {
            strFilter = strFilter.substring(2, 0);
        }
        /*length filter*/
        else if (strFilter.length() == 8) {
            String binString = String.format("%32s",
                Integer.toBinaryString(Define.fixedParseInt(strFilter,16)));
            return binString.replace(' ','0');
        } 
        else if (strFilter.length() == 32) {
            return strFilter;
        }
        else {
            System.out.println("code type error");
            return String.format("%032d",0);
        }
        return String.format("%032d",0);
    }

    /**
     * 32 bit binary code split
     * @param codeString : 32 bit machinecode
     * @param splitIndexs : the endspace of each split
     */
    public static List<String> splits(String codeString,int... splitIndexs) {
        List<String> ans = new ArrayList<String>();
        int former = 0;
        for (int latter : splitIndexs) {
            ans.add(codeString.substring(former,latter));
            former = latter;
        }
        return ans;
    }

    /**
     * disassemble code split
     */
    public static String[] disSplits(String codeString,int limit) {
        return codeString.split("[\\s,()]+",limit);
    }

    public static String[] disSplits(String codeString) {
        return codeString.split("[\\s,()]+");
    }
}



// TODO:以下保留，没想好
// import java.io.File;
// import java.util.ArrayList;
// import java.util.HashMap;
// import java.util.List;
// import java.util.Map;

// public class Regist {
    
//     private static Map<String,String> funcs = new HashMap<String,String>();

//     /**get class by package
//      * 
//      */
//     private static List<String> getClassByPkg(String PkgName) {
//         String filePath = PkgName.replace('.', '/');
//         File file = null;
//         if (filePath != null) {
//             file = new File(filePath);
//         }    

//         ArrayList<String> filesname = new ArrayList<String>();
//         try {
//             File[] files = file.listFiles();
//             for (File file2 : files) {
//                 if (file2.isFile()) {
//                     filesname.add(file2.getName());
//                 }
//             }
            
//             return filesname;
//         } catch (Exception e) {
//             System.out.println("Package visit fault");
//             return null;
//         }
//     }

//     static{
//         List<String> classNames = getClassByPkg("functions");
//         for (String file : classNames) {
            
//         }
//     }

// }
